﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deflection
{
    class Wave
    {
        private Path path = new Path();

        private List<Enemy> enemies = new List<Enemy>();



        private Enemy.EnemyType enemyType;

        int numEnemies = 0;
        float spawnInterval = 0.0f;
        int waveNumber = 0;
        
        EnemyFactory eFactory;
        private int accumulatedMilliSeconds = 0;
        EnemyFactory.ScreenSide spawnSide = EnemyFactory.ScreenSide.All;

        public int WaveNumber { get; set; }
        public bool WaveComplete { get; set; }
        public int NumOfEnemies { get; set; }

        public List<SubWave> subWaves = new List<SubWave>();

        public Wave(int waveNumber, float spawnInterval)
        {
            //this.path = path;
            this.eFactory = EnemyFactory.Instance();
            //this.numEnemies = numEnemies;
            //this.enemyType = enemyType;
            this.spawnInterval = spawnInterval;
            this.waveNumber = waveNumber;
            //this.spawnSide = spawnSide;
        }



        public void Update(GameTime gameTime)
        {
            this.eFactory = EnemyFactory.Instance();
            accumulatedMilliSeconds += gameTime.ElapsedGameTime.Milliseconds;
            if (accumulatedMilliSeconds >= 50 && numEnemies < waveNumber * 2)
            {
                Random rand = new Random();
                // testing
                int maxEnum = (int)Enum.GetValues(typeof(Enemy.EnemyType)).Cast<Enemy.EnemyType>().Max();
                Enemy.EnemyType type = (Enemy.EnemyType)rand.Next(0, maxEnum);
                int r = rand.Next(0, 3);
                if (r == 0)
                {
                    eFactory.CreateEnemy(EnemyFactory.ScreenSide.Right, Enemy.EnemyType.SquareEnemy);
                    numEnemies++;
                }
                else if (r == 1)
                {
                    eFactory.CreateEnemy(EnemyFactory.ScreenSide.Right, Enemy.EnemyType.HexagonEnemy);
                    numEnemies++;
                }
                else
                {
                    eFactory.CreateEnemy(EnemyFactory.ScreenSide.Right, Enemy.EnemyType.TriangleEnemy);
                    numEnemies++;
                }
                if (eFactory.MakeSuperSquare() || eFactory.MakeSuperHexagon() || eFactory.MakeSuperTriangle())
                {
                    numEnemies++;
                }
                
                accumulatedMilliSeconds = 0;

            }
            if (numEnemies == waveNumber * 2 && eFactory.Enemies.Count == 0)
            {
                WaveComplete = true;
            }
            //if (gameTime.TotalGameTime.Milliseconds % spawnInterval == 0 && enemies.Count < numEnemies)
            //{
            //    //CreateEnemy(new Vector2(1700, rand.Next(0, (int)GameParameters.SCREEN_SIZE.Y)), Enemy.EnemyType.SquareEnemy);
            //    Enemy newEnemy = EnemyFactory.Instance().CreateEnemy(spawnSide, enemyType);
            //    newEnemy.path = this.path;
            //    enemies.Add(newEnemy);               
            //}
            ////testing moving around paths
            ////this.path.scale = MathHelper.Lerp(this.path.scale, 0.0f, 0.001f);

            //Vector2 dir = new Vector2();
            //dir = Vector2.Subtract(InputHandler.MouseAsVector2, this.path.position);
            //dir.Normalize();

            //this.path.position = Vector2.Add(this.path.position, dir);

            /*foreach (Enemy enemy in enemies)
            {
                if (enemy.Alive)as
                {
                    enemy.Update(gameTime);
                }
                else
                {
                    enemies.Remove(enemy);
                    if (enemies.Count == 0)
                    {
                        waveComplete = true;
                    }
                }
            }*/

            //foreach (SubWave subWave in subWaves)
            //{
            //    subWave.Update(gameTime);
            //}


            // Create Sub Waves
            
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

        }

        public void SpawningAlgo()
        {
            // Look at wave number
            // Convert that to some form of difficulty
            // Work out out number of enemies
            // work out spawn interval
            // work out what kinds of enemies
            // work out formations
            // work out paths
            // work out super enemies

        }

        /// <summary>
        /// Spawn a Path
        /// </summary>
        private void Spawn()
        {

        }

        /// <summary>
        /// Spawn a Formation
        /// </summary>
        /// <param name="a"></param>
        private void Spawn(int a)
        {

        }
    }
}
